using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ca.Armstrong.ProjectEuler
{
    class SolutionRunner
    {
        static void Main(string[] args)
        {
            Problem7 problem7 = new Problem7();
            problem7.solve();
            Console.WriteLine("Press ENTER to continue...");
            Console.ReadLine();            
        }
    }

    class Problem7
    {
        private const int INCREMENT_SKIPPING_EVEN_NUMBERS = 2;
        private const int FIRST_KNOWN_PRIME = 2;
        private const int SECOND_KNOWN_PRIME = 3;
        private const int THIRD_KNOWN_PRIME = 5;

        private int currentNumber = THIRD_KNOWN_PRIME;
        private List<int> primeNumberAccumulator = new List<int>(10001);

        public void solve()
        {
            initializeAccumulatorWithKnownPrimeNumbers();
            int primeNumberCount = getPrimeNumberCountFromConsole();
            accumulateAPrimeNumberSet(primeNumberCount);
            reportResultsToConsole();
        }
        
        private void initializeAccumulatorWithKnownPrimeNumbers()
        {
            primeNumberAccumulator.Add(FIRST_KNOWN_PRIME);
            primeNumberAccumulator.Add(SECOND_KNOWN_PRIME);
            primeNumberAccumulator.Add(THIRD_KNOWN_PRIME);
        }

        private int getPrimeNumberCountFromConsole()
        {
            Console.WriteLine("Enter nth Prime Number to find.");
            Console.WriteLine("(n > 3)");
            while (true)
            {
                try
                {
                    string line = Console.ReadLine();
                    return int.Parse(line);
                }
                catch
                {
                    Console.WriteLine("Enter an actual number.");
                }
            }
        }

        private void accumulateAPrimeNumberSet(int maximumPrimeNumberCount)
        {
            while (primeNumberAccumulator.Count < maximumPrimeNumberCount)
            {
                if (isCurrentNumberPrime())
                {
                    primeNumberAccumulator.Add(currentNumber);
                }
                currentNumber += INCREMENT_SKIPPING_EVEN_NUMBERS;
            }
        }

        private void reportResultsToConsole()
        {
            Console.WriteLine("Problem seven's solution is: " + primeNumberAccumulator[primeNumberAccumulator.Count - 1]);
        }
        
        private bool isCurrentNumberPrime()
        {
            int divisorsCount = primeNumberAccumulator.Count;
            for (int divisorsIndex = 1; divisorsIndex < divisorsCount; divisorsIndex++) 
            {
                if (isCurrentNumberDivisibleWith(primeNumberAccumulator[divisorsIndex]))
                {
                    return false;
                }
            }
            return true;
        }

        private bool isCurrentNumberDivisibleWith(int divisor)
        {
            return currentNumber % divisor == 0;
        }
    }
}